package com.tjzx.struts.question;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import com.tjzx.common.Cons;
import com.tjzx.common.Pager;
import com.tjzx.common.Utils;
import com.tjzx.entity.EipQuestionInfo;
import com.tjzx.service.EipQuestionInfoService;

public class EipQuestioninfoAction extends ActionSupport {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		WebApplicationContext ctx = this.getWebApplicationContext();
		EipQuestionInfoService ts = (EipQuestionInfoService)ctx.getBean("eipQuestionInfoService");
		String method = request.getParameter("method");
		if (Utils.isEmpty(method)) {
			return mapping.findForward("error");
		} else if (method.equals("showList")){
			return executeShowList(mapping, form, request, response, ts);
		} else if (method.equals("show")){
			return executeShow(mapping, form, request, response, ts);
		} else if (method.equals("add")){
		   executeAdd(mapping, form, request, response, ts);
		   return executeShowList(mapping, form, request, response, ts);
		} else if (method.equals("do")){
			return mapping.findForward("success");
		}
		return mapping.findForward("error");
	}
	public ActionForward executeShowList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipQuestionInfoService ts) {
		String pageNo = request.getParameter("pageNo");
		
		//calculate the page no
		int currPageNo = 1;
		if (Utils.isNotEmpty(pageNo)) {
			currPageNo = Integer.valueOf(pageNo);
		}
		Pager<EipQuestionInfo> pager = null;
		pager = ts.getSuggestions(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			
		request.setAttribute("pageNo", pager.getPageNo());
		request.setAttribute("totalPages", pager.getTotalPages());
		request.setAttribute("totalRows", pager.getRowCount());
		request.setAttribute("suggestsList", pager.getList());
		return mapping.findForward("showList");
	}
	public ActionForward executeShow(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipQuestionInfoService ts) {
		String id = request.getParameter("id");
		EipQuestionInfo question = ts.getEipQuestionInfoById(Integer.parseInt(id));
		request.setAttribute("question", question);
		return mapping.findForward("show");
	}
	public void executeAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipQuestionInfoService ts) {
		String user = request.getParameter("user");
		String title = request.getParameter("title");
		String tel = request.getParameter("tel");
		String content = request.getParameter("content");
		EipQuestionInfo question =new EipQuestionInfo();
		question.setQuestionPersonName(user);
		question.setQuestionTitle(title);
		question.setTel(tel);
		question.setAnswerContent(content);
		ts.addQuestionInfo(question);
	}
}